/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;

namespace GamePlayer.Tools.Graphics.Imaging
{
	[ContentImporter( ".boguscrap", DisplayName = "Sky Cube Importer", DefaultProcessor = "TextureProcessor" )]
	public class SkyCubeImporter : ContentImporter<TextureCubeContent>
	{
		private static readonly string[] SideCodes = new string[] { "_ft", "_bk", "_up", "_dn", "_rt", "_lf" };

		public override TextureCubeContent Import( string filename, ContentImporterContext context )
		{
			TextureCubeContent ret = new TextureCubeContent();

			string ext = Path.GetExtension( filename );
			string basePath = Path.ChangeExtension( filename, null );
			foreach( string s in SideCodes )
			{
				if( basePath.EndsWith( s ) )
				{
					basePath = basePath.Substring( 0, basePath.Length - s.Length );
					break;
				}
			}

			TextureImporter importer = new TextureImporter();

			for( int i = 0; i < SideCodes.Length; i++ )
			{
				string imgBasePath = Path.ChangeExtension( basePath + SideCodes[i], ext );

				context.Logger.LogMessage( "Importing cube face {0}.", imgBasePath );

				TextureContent content = importer.Import( imgBasePath, context );
				context.AddDependency( imgBasePath );

				ret.Faces[i].Add( content.Faces[0][0] );
			}

			return ret;
		}
	}
}
